//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_uncon.sqf"
#include "xr_macros.sqf"

private ["_pisdead", "_lives", "_disp", "_curtime", "_ott", "_respawn", "_tt", "_etime", "_dcounter", "_typer", "_remactions", "_curanim", "_nextcrytime", "_pl_died", "_xxstarttime", "_plposm"];
PARAMS_1(_typer);
__TRACE_1("","_typer");
disableSerialization;
enableRadio false;
__pSetVar [QGVARXR(hasusedmapclickspawn), false];
#ifdef __TT__
[QGVARXR(addActions), [player, side (group player)]] call d_fnc_NetCallEventToClients;
#else
[QGVARXR(addActions), player] call d_fnc_NetCallEventToClients;
#endif
if (!captive player) then {
	[QGVARXR(setCap), [player,true]] call d_fnc_NetCallEvent;
};

GVARXR(respavailablemarker) = false;

#define __dspctrl(ctrlid) (_disp displayCtrl ctrlid)
closeDialog 0;
GVARXR(respawn_available) = false;
_curtime = time;
__pSetVar [QGVARXR(unconendtime), _curtime + GVARXR(lifetime)];
_ott = -1;
_respawn = if (GVARXR(respawn_available_after) != -1) then {
	_curtime + GVARXR(respawn_available_after)
} else {
	GVARXR(respawn_available) = true; -1
};

__TRACE_2("","_curtime","_respawn");

if !(GVARXR(followcam)) then {
	createDialog QGVARXR(UnconDlg);
	_disp = __uiGetVar(GVARXR(UnconDlg));
	__dspctrl(1000) ctrlShow false;
	__dspctrl(1001) ctrlShow false;
} else {
	[true] spawn FUNCXR(spectating);
};

#define __ucdlg1005 (__uiGetVar(GVARXR(UnconDlg)) displayCtrl 1005)
#define __ucdlg1006 (__uiGetVar(GVARXR(UnconDlg)) displayCtrl 1006)
#define __spectdlg1005 (__uiGetVar(GVARXR(SpectDlg)) displayCtrl 1005)
#define __spectdlg1006 (__uiGetVar(GVARXR(SpectDlg)) displayCtrl 1006)
if (_respawn == -1) then {
	if (!GVARXR(followcam)) then {
		__ucdlg1006 ctrlSetText (localize "STR_DOM_MISSIONSTRING_922");
		__ucdlg1006 ctrlSetTextColor [1,1,0,1];
		__ucdlg1006 ctrlCommit 0;
	};
	if (!GVARXR(respavailablemarker)) then {
		call FUNCXR(respawn_markers);
	};
};
_pl_died = false;
_dcounter = 0;
__TRACE("starting main loop");
_remactions = false;
_nextcrytime = time + 15 + (random 15);
#ifdef __DEBUG__
_alipl = alive player;
_uncipl = __pGetVar(GVARXR(pluncon));
__TRACE_2("","_alipl","_uncipl");
#endif
if (GVARXR(with_marker)) then {
	__TRACE("creating marker");
	[QGVARXR(umarker), [player, getPosASL player]] call d_fnc_NetCallEvent;
};
_xxstarttime = time + 40;
_plposm = getPosASL player;
__TRACE_1("","_plposm");
while {alive player && {__pGetVar(GVARXR(pluncon))}} do {
	if (GVARXR(withSounds) && {time >= _nextcrytime}) then {
		_plsayer = floor (random 4);
		_nummoans = floor (random (count ((GVARXR(moansoundsar) select _plsayer) select 1)));
		__TRACE_2("next say","_plsayer","_nummoans");
		//[QGVARXR(say3d), [player, _plsayer, _nummoans, getPos player]] call d_fnc_NetCallEventToClients;
		playSound3D ["a3\sounds_f\characters\human-sfx\" + ((GVARXR(moansoundsar) select _plsayer) select 0) + "\" + (((GVARXR(moansoundsar) select _plsayer) select 1) select _nummoans), player, false, getPos player, 1, 1, 100];
		_nextcrytime = time + 15 + (random 15);
	};
	_tt = round (__pGetVar(GVARXR(unconendtime)) - time);
	if (_tt != _ott) then {
		if (!GVARXR(followcam)) then {
			__ucdlg1005 ctrlSetText str _tt;
		} else {
			__spectdlg1005 ctrlSetText str _tt;
		};
		_ott = _tt;
	};
	if (GVARXR(near_player_dist_respawn) && {!GVARXR(respawn_available)} && {_dcounter > 10} && {time > _xxstarttime}) then {
		_nearunit = objNull;
		{
			_xm = __getMNsVar2(_x);
			if (!isNil "_xm" && {_x != GVARXR(strpl)} && {!isNull _xm} && {!__GV(_xm,GVARXR(pluncon))} && {_xm distance player < GVARXR(near_player_dist)}) exitWith {
				_nearunit = _xm;
			};
		} forEach GVARXR(player_entities);
		if (isNull _nearunit) then {
			GVARXR(respawn_available) = true;
			if (!GVARXR(respavailablemarker)) then {
				call FUNCXR(respawn_markers);
			};
			if (!GVARXR(followcam)) then {
				__ucdlg1006 ctrlSetText (localize "STR_DOM_MISSIONSTRING_922");
				__ucdlg1006 ctrlSetTextColor [1,1,0,1];
				__ucdlg1006 ctrlCommit 0;
			} else {
				__spectdlg1006 ctrlSetText (localize "STR_DOM_MISSIONSTRING_922");
				__spectdlg1006 ctrlSetTextColor [1,1,0,1];
				__spectdlg1006 ctrlCommit 0;
			};
		};
		_dcounter = 0;
	} else {
		__INC(_dcounter);
		if (_dcounter > 1000) then {_dcounter = 0};
	};
	if (_respawn != -1 && {!GVARXR(respawn_available)} && {time >= _respawn}) then {
		if (!GVARXR(followcam)) then {
			__ucdlg1006 ctrlSetText (localize "STR_DOM_MISSIONSTRING_922");
			__ucdlg1006 ctrlSetTextColor [1,1,0,1];
			__ucdlg1006 ctrlCommit 0;
		} else {
			__spectdlg1006 ctrlSetText (localize "STR_DOM_MISSIONSTRING_922");
			__spectdlg1006 ctrlSetTextColor [1,1,0,1];
			__spectdlg1006 ctrlCommit 0;
		};
		GVARXR(respawn_available) = true;
		if (!GVARXR(respavailablemarker)) then {
			call FUNCXR(respawn_markers);
		};
	};
	if (_tt <= 0) exitWith {
		__TRACE("_tt < =0, exit");
		if (GVARXR(with_marker)) then {
			[QGVARXR(delml), GVARXR(strpldead)] call d_fnc_NetCallEvent;
		};
		[QGVARXR(removeActions), player] call d_fnc_NetCallEventToClients;
		_remactions = true;
		__TRACE("_tt <= 0, black out");
		172 cutText [(localize "STR_DOM_MISSIONSTRING_932"),"BLACK OUT", 1];
		sleep 1;
		closeDialog 0;
		__TRACE("_tt stopspect = true");
		GVARXR(stopspect) = true;
		sleep 1.3;
		#ifndef __TT__
		_posbase = markerPos "base_spawn_1";
		#else
		_posbase = if (GVARXR(side_pl) == blufor) then {markerPos "base_spawn_1"} else {markerPos "base_spawn_2"};
		#endif
		_posbase set [2, 0];
		player setPos _posbase;
		player setDamage 0;
		[QGVARXR(wn), [player,105]] call d_fnc_NetCallEvent;
		if (!isNil "d_custom_layout" && {count d_custom_layout > 0}) then {
			call d_fnc_retrieve_layoutgear;
		};
		sleep 1.3;
		__TRACE("time over black in");
		172 cutText ["","BLACK IN", 2];
		GVARXR(pl_has_pos_changed) = false;
		player setVariable [QGVARXR(pluncon), false, true];
		if (GVARXR(max_lives) != -1) then {
			0 spawn {
				sleep 3;
				hintSilent format [(localize "STR_DOM_MISSIONSTRING_933"), __pGetVar(GVARXR(lives))];
			};
		};
		// deactivated for now
		//if (!GVARXR(no_respawn_dialog)) then {
		//	createDialog QGVARXR(RespDlg);
		//};
		//0 spawn FUNCXR(resp_dlg_helper);
		_pl_died = true;
	};
	if (GVARXR(with_marker)) then {
		// adjust marker if uncon player gets moved. Can happen when he gets dragged/carried or when a shilka hits him (flying miles away)
		_newplposm = getPosASL player;
		/*#ifdef __DEBUG__
		_distdiff = _newplposm distance _plposm;
		__TRACE_1("","_distdiff");
		#endif*/
		if (_newplposm distance _plposm > 5) then {
			__TRACE_2("","_plposm","_newplposm");
			_plposm = _newplposm;
			[QGVARXR(movemarker), [player, getPosASL player]] call d_fnc_NetCallEvent;
		};
	};
	if (GVARXR(respavailablemarker)) then {
		call FUNCXR(animateRespMarkers);
	};
	sleep 0.02;
	// #ifdef __DEBUG__
	// _alipl = alive player;
	// _uncipl = __pGetVar(GVARXR(pluncon));
	// __TRACE_2("in loop","_alipl","_uncipl");
	// #endif
};
__TRACE("main loop end");
if (GVARXR(respavailablemarker)) then {
	__TRACE("deleteRespMarkers");
	call FUNCXR(deleteRespMarkers);
};
if (!_remactions) then {
	__TRACE("_remactions");
	[QGVARXR(removeActions), player] call d_fnc_NetCallEventToClients;
};
if (GVARXR(with_marker)) then {
	__TRACE("delml");
	[QGVARXR(delml), GVARXR(strpldead)] call d_fnc_NetCallEvent;
};
__TRACE("set capture false player");
[QGVARXR(setCap), [player,false]] call d_fnc_NetCallEvent;
enableRadio true;
__TRACE_1("","_pl_died");
if (!_pl_died) then {
	if (!__pGetVar(GVARXR(hasusedmapclickspawn))) then {
		__TRACE("player alive and lives left, black out");
		172 cutText ["","BLACK OUT", 1];
	};
	sleep 1;
	if (!__pGetVar(GVARXR(hasusedmapclickspawn))) then {
		[QGVARXR(wn), [player,102]] call d_fnc_NetCallEvent;
	};
	player setDamage 0;
	closeDialog 0;
	if (!GVARXR(stopspect)) then {
		__TRACE("stopspect = true");
		GVARXR(stopspect) = true;
	};
	if (!__pGetVar(GVARXR(hasusedmapclickspawn))) then {
		__TRACE("player alive and lives left, black in");
		172 cutText ["","BLACK IN", 6];
		if (GVARXR(max_lives) != -1) then {
			0 spawn {
				sleep 7;
				hintSilent format [(localize "STR_DOM_MISSIONSTRING_933"), __pGetVar(GVARXR(lives))];
			};
		};
	};
};
player setFatigue 0;
BFUNC(feedback_burningTimer) = 0;
call FUNCXR(joingr);
if (d_WithAcre) then {
	[false] call acre_api_fnc_setSpectator;
};
BFUNC(feedback_allowPP) = true;
__TRACE("end");